extends CharacterBody2D

@export var roleTag = "鬼人正邪"
var SPEED = 100
var rolespeed = 4.5
var speedBuff = 0
var sprite:AnimatedSprite2D
var DF:bool = false
var Fire:int = 1
var 子弹列表:Array
var 攻击间隔 = 10
var atktimeint = 10
var scaleSize = Vector2(3.0,3.0)
var 角色单独事件:Node
var isShift:bool = false

var 角色道具列表:Array[Node2D]=[]
var 道具提供速度:int = 0
func _ready() -> void:
	sprite= $AnimatedSprite2D
	var sprite_frame:SpriteFrames = load("res://Files/Player/"+roleTag+"/"+roleTag+".tres")
	scale = scaleSize
	角色单独事件 = load("res://Code/Player/角色单独事件.tscn").instantiate()
func LoadRole(tag:String ="鬼人正邪"):
	roleTag = tag
	sprite= $AnimatedSprite2D
	var frames:SpriteFrames = load("res://Files/Player/"+roleTag+"/"+roleTag+".tres")
	sprite.sprite_frames = frames
	
func _physics_process(delta: float) -> void:
	if Fire ==1:
		攻击间隔 = 4
	if Fire ==2:
		攻击间隔 = 7
	刷新状态()
	
	# As good practice, you should replace UI actions with custom gameplay actions.
	var directiony = 0
	if Input.is_action_pressed("上"):
		directiony += -1
	if Input.is_action_pressed("下"):
		directiony += 1
	if directiony:
		velocity.y = directiony * (SPEED * (rolespeed + speedBuff) + 道具提供速度)
	else:
		velocity.y = move_toward(velocity.y, 0, SPEED)
		pass
	var directionx := Input.get_axis("ui_left", "ui_right")
	if directionx:
		velocity.x = directionx * (SPEED * (rolespeed + speedBuff) + 道具提供速度)
		DF =false
		if sprite.animation =="待机":
			if velocity.x>0 :
				sprite.play("向右移动")
			else:
				sprite.play("向左移动")
		if sprite.animation =="左" or sprite.animation =="向左移动":
			if Input.is_action_pressed("右"):
				if velocity.x>0 :
					sprite.play("向右移动")
		if sprite.animation =="右" or sprite.animation =="向右移动":
			if Input.is_action_pressed("左"):
				if velocity.x<0 :
					sprite.play("向左移动")
			
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		if is_zero_approx(velocity.x) and sprite.animation!="待机":
			if sprite.animation =="左":
				sprite.play("向左移动")	
			if sprite.animation =="右":
				sprite.play("向右移动")
			if sprite.animation =="向左移动":
				sprite.play_backwards("向左移动")
				DF =true
			if sprite.animation =="向右移动":
				sprite.play_backwards("向右移动")
				DF =true
	if Input.is_action_pressed("发射"):
		if atktimeint ==0:
			手动发射子弹()
			atktimeint = 攻击间隔
	$AnimatedSprite2D/EffSloweffect2.visible = Input.is_action_pressed("瞄准")
	$AnimatedSprite2D/EffSloweffect.visible = Input.is_action_pressed("瞄准")
	if Input.is_action_pressed("瞄准"):
		
		speedBuff = -2.5
		isShift = true
	else:
		speedBuff = 0
		isShift = false
	if isShift:
		按住Shift时()
	move_and_slide()
func 按住Shift时():
	#按住Shift之后执行的代码
	
	
	pass
func 手动发射子弹():
	角色单独事件.get_node(roleTag).手动发射子弹(self)
	for 道具 in 角色道具列表:
		if 道具.has_method("道具主动效果"):
			道具.道具主动效果(self)
			
	
	
func 刷新状态():
	$AnimatedSprite2D/EffSloweffect.rotation_degrees+=3
	$AnimatedSprite2D/EffSloweffect2.rotation_degrees-=3
	if atktimeint >0:
		atktimeint-=1
	道具提供速度 = 0
	for 道具 in 角色道具列表:
		if 道具.has_method("道具被动效果"):
			道具.道具被动效果(self)
		print(道具.速度)
		道具提供速度 += 道具.速度
func _on_animated_sprite_2d_animation_finished() -> void:
	if DF:
		sprite.speed_scale = 2
		sprite.play("待机")
		DF =false
	else:
		if sprite.animation == "向左移动":
			sprite.play("左")
		if sprite.animation == "向右移动":
			sprite.play("右")
func 被打(弹幕:Node2D):
	
	pass # Replace with function body.
